package com.tjvib.view.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;

import com.tjvib.R;
import com.tjvib.adapter.MessageAdapter;
import com.tjvib.base.BaseActivity;
import com.tjvib.bean.MessageBean;
import com.tjvib.presenter.BasePresenter;
import com.tjvib.util.Manager.UserManager;
import com.tjvib.util.PressUtil;
import com.tjvib.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends BaseActivity {

    private RecyclerView chat_rv;
    private EditText advise_et_con;
    private ImageView chat_iv_send;

    private MessageAdapter messageAdapter;
    private List<MessageBean> messageBeanList = new ArrayList<>();
    private String sorry = "您好，我是TJVIB客服，很高兴为您服务。此部分功能为Beta版暂未开发，请使用拨打电话/发送短信/意见反馈联系，感谢您的使用。";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_chat;
    }

    @Override
    protected boolean initView() {
        chat_rv = findViewById(R.id.chat_rv);
        advise_et_con = findViewById(R.id.advise_et_con);

        chat_iv_send = findViewById(R.id.chat_iv_send);
        chat_iv_send.setOnClickListener(v -> {
            String con = advise_et_con.getText().toString();
            if (con.equals("")) {
                ToastUtil.show("不能发送空白内容哦");
            } else {
                messageBeanList.add(new MessageBean(con, true));
                messageBeanList.add(new MessageBean(sorry, false));
                messageAdapter.notifyItemInserted(messageBeanList.size() - 1);
                chat_rv.scrollToPosition(messageBeanList.size() - 1);
                advise_et_con.setText("");
            }
        });

        PressUtil.setPressChange(ChatActivity.this, chat_iv_send);

        return true;
    }

    @Override
    protected boolean initData() {
        messageBeanList = new ArrayList<>();
        messageBeanList.add(new MessageBean(sorry, false));

        messageAdapter = new MessageAdapter(R.layout.item_message, messageBeanList, ChatActivity.this);
        messageAdapter.setIcon_url(UserManager.getInstance().getImage());
        messageAdapter.setClient_url("https://img.tukuppt.com/png_preview/00/10/91/AlwLLuRr4q.jpg!/fw/780");

        chat_rv.setLayoutManager(new LinearLayoutManager(this));
        LinearSnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(chat_rv);

        chat_rv.setAdapter(messageAdapter);

        return true;
    }

    @Override
    protected BasePresenter genPresenter() {
        return null;
    }
}